<?php
namespace App\Logics;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\SuggestionModel;

//逻辑处理
class SuggestionLogic extends BaseLogic {


    public function __construct()
    {
        parent::__construct();

    }

    public function index(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $phone=isset($para['phone'])?$para['phone']:'';
        $name=isset($para['name'])?$para['name']:'';
        $start_time=isset($para['start_time'])?$para['start_time']:'';
        $end_time=isset($para['end_time'])?$para['end_time']:'';

        $where = $searchWhere = [];
        $whereRaw='';$whereCol=[];

        if ($name) {
            $where['name'] = $name;
            $searchWhere[] = ['b.name', 'like', $name . '%'];
        }
        if ($phone) {
            $where['phone'] = $phone;
            $searchWhere[] = ['suggestion.phone', 'like', $phone . '%'];
        }

        if ($start_time) {
            $where['start_time'] = $start_time;
            $whereRaw.="suggestion.create_time > ? ";
            $whereCol[]=$start_time;
        }

        if ($end_time) {
            $where['end_time'] = $end_time;
            if($whereRaw)$whereRaw.=" and ";
            $whereRaw.="suggestion.create_time <  ? ";
            $whereCol[]=$end_time;
        }

        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }

        $model=SuggestionModel::leftJoin('user as b','suggestion.phone','=','b.phone')
            ->select("suggestion.id","suggestion.user_id","suggestion.phone","suggestion.create_time","suggestion.suggestion_text",'b.name')
            ->where($searchWhere);
        if($whereRaw)$model->whereRaw($whereRaw,$whereCol);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');

        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url,

        ];
        return $arr;
    }

    public static function getSuggestion($fields=['id','name']){

        $where=[

        ];
        $data= SuggestionModel::where($where)->select($fields)->get();
        return ob_to_array($data);
    }

    //判断该数据是否存在
    public static function isOne($id){
        $id=(int)$id;
        return SuggestionModel::where("id",$id)->count();
    }

    //获得一条数据
    public static function getOne($id,$fields=['*']){
        $id=(int)$id;
        $data= SuggestionModel::where("id",$id)->first();
        return ob_to_array($data);
    }





}
